﻿using MediatR;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.LiveAnimalsa
{
    public class LiveAnimalsShowtwosHandler : IRequestHandler<LiveAnimalsShowCommand, Result>
    {
        IBaseRepositoryGet<LiveAnimals> LiveAnimalsRep;
        IBaseRepositoryGet<CheckIn> CheckInRep;
        IBaseRepositoryGet<Supplier> SupplierRep;
        public LiveAnimalsShowtwosHandler(IBaseRepositoryGet<LiveAnimals> liveAnimalsRep, IBaseRepositoryGet<CheckIn> checkInRep, IBaseRepositoryGet<Supplier> supplierRep)
        {
            LiveAnimalsRep = liveAnimalsRep;
            CheckInRep = checkInRep;
            SupplierRep = supplierRep;
        }
        /// <summary>
        /// 活畜秤重显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(LiveAnimalsShowCommand request, CancellationToken cancellationToken)
        {

            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in LiveAnimalsRep.GetAll()
                            join b in CheckInRep.GetAll()
                            on a.CheckInId equals b.CheckInId
                            join c in SupplierRep.GetAll()
                            on b.SupplierId equals c.SupplierId
                            select new
                            {
                                a.LiveAnimalsId,
                                a.LiveAnimalsDate,
                                a.LiveAnimalsDocument,
                                a.CheckInId,
                                c.SupplierName,
                                b.CarNo,
                                b.SupplierId,
                                b.CheckInBatch,
                                b.ApproachNum,
                                a.LiveAnimalsWeight,
                                a.LiveAnimalsRAverageWeight,
                            }).ToList();
                // 活畜称重单时间
                if (request.LiveAnimalsDate != null)
                {
                    list = list.Where(x => x.LiveAnimalsDate >= request.LiveAnimalsDate).ToList();
                }
                // 供应商/养殖户 外健
                if (request.SupplierId > 0)
                {
                    list = list.Where(x => x.SupplierId == request.SupplierId).ToList();
                }
                //进场批次
                if (!string.IsNullOrEmpty(request.CheckInBatch))
                {
                    list = list.Where(x => x.CheckInBatch.Contains(request.CheckInBatch)).ToList();
                }
                //总条数
                var totalcount = list.Count();
                //总页数
                var pagecount = (int)Math.Ceiling(totalcount * 1.0 / request.pagesize);
                //分页
                list = list.OrderBy(x => x.LiveAnimalsId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                //返回元组
                var CheckInData = new
                {
                    totalcount,
                    pagecount,
                    list
                };

                //如果有数据就返回
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = CheckInData;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
